/**
 * Created with PhpStorm.
 * User: Administrator
 * Date: 14-1-16
 * Time: 下午4:01
 * Function:
 */
$(function(){
    $("#mem_list_tbd").on('dblclick','td', function(){
        if($(this).hasClass('td_dname')) {
            $(this).find('input.dname_input')
                .removeAttr('readonly')
                .addClass('edit_dname')
                .focus();
            return;
        }
        var tr = $(this).parent('tr');
        if(tr.hasClass('hightlight')) {
            tr.removeClass('hightlight');
        } else {
            tr.addClass('hightlight');
        }
    });
    var guid = 0;
    var save = function(){
        var data = {
            action:'DnameUpdate',
            dname:$("#uname").val(),
            com_type: $("#com_type").find('option:selected').val(),
            dcode: $("#dcode").val(),
            ast : $("#add_saler_type").find('option:selected').val(),
            jiutian_auth : $('input[name="jiutian_auth"]:checked').val(),
            refund_back : $('input[name="refund_back"]:checked').val(),
            id: guid
        };
//        console.log(data);return;
        if(guid>0 && $("#uname").val().length>0) {
            $.post('/call/jh_mem.php',data, function(res){
                alert(res.msg);
                if(res.status=='ok') {
                    location.reload();
                }
            },'json');
        } else {
            alert("发生错误。企业名称不能为空。")
        }

    };
    $("#mem_list_tbd").on('click','a.edit', function(){
        guid = $(this).attr('data-id');
        var com_types = ['--请选择--','景区','酒店','旅行社','加盟门店','电商','团购网','淘宝/天猫','其他'],
            astList = ['新建','搜索/新建'],
            uname = $("#editInfo_"+guid).find('span.dname').text(),
            old_value = $("#editInfo_"+guid).find('span.com_type').text(),
            ast = parseInt($("#editInfo_"+guid).attr('data-ast'),10),
            dcode = $("#dcode_"+guid).text(),
            jt_acc = $(this).parent().next().text(),
            acc_right = parseInt($("#editInfo_"+guid).attr('data-jt-acc'),10),
            refund_back_z = parseInt($("#editInfo_"+guid).attr('data-refund_back'),10),
            content = '<p>企业名称：<input class="pinput-txt" type="text" id="uname" value="' + $.trim(uname) + '" /></p>',
            options = '',
            options2 = '';
        for(var i=0;i < com_types.length;i++) {
            options += '<option value="'+com_types[i]+'"'+
                (com_types[i]==old_value? 'selected="selected"':'')+'>'+com_types[i]+'</option>';

        }
        for(var j=0;j < astList.length;j++) {
            options2 += '<option value="'+j+'"'+
                (j==ast ? 'selected="selected"':'')+'>'+astList[j]+'</option>';

        }
        content += '<p>企业类型：<select id="com_type">'+options+'</select></p>';
        content += '<p>接口编码：<input class="pinput-txt" type="text" id="dcode" value="'+dcode+'"></p>';
        content += '<p>添加分销：<select id="add_saler_type">'+options2+'</select></p>';
        if(jt_acc=='供应商'){
            content += '<p>绑定闸机权限：<input type="radio"  name="jiutian_auth" value="1" '+(acc_right==1?'checked':'')+'/>允许';
            content += '<input type="radio"  name="jiutian_auth" value="0" '+(acc_right==0?'checked':'')+'/>禁止</p>';
            
            content += '<p>分销商/散客退款设置：<input type="radio"  name="refund_back" value="1" '+(refund_back_z==1?'checked':'')+'/>支付宝';
            content += '<input type="radio"  name="refund_back" value="0" '+(refund_back_z==0?'checked':'')+'/>资金账户</p>';
         }
        easyDialog.open({
            container : {
            header : '信息修改',
            content : content,
            yesFn : save,
            noFn : true
            }
        });
    });
	
	//账户设置 
	
	
	
	$("#mem_list_tbd").on('click','a.account_set', function(){
		var com_types = ['--请选择--','正常','禁用'],
            astList = ['无','超过钱款金额','超过钱款时长','套餐过期','其他'],
			uname = $("#editInfo_"+guid).find('span.dname').text(),
			old_value = $("#editInfo_"+guid).find('span.com_type').text(),
			ast = parseInt($("#editInfo_"+guid).attr('data-ast'),10),
			options = '',
			options2 = '';
			for(var i=0;i < com_types.length;i++) {
				options += '<option value="'+i+'"'+
					(com_types[i]==old_value? 'selected="selected"':'')+'>'+com_types[i]+'</option>';

			}
			for(var j=0;j < astList.length;j++) {
				options2 += '<option value="'+j+'"'+
					(j==ast ? 'selected="selected"':'')+'>'+astList[j]+'</option>';

			}
			guid = $(this).attr('data-id'); 
			var check = {
				action:'get_info',
				id: guid
			};
			if(guid>0) {
				$.post('http://www.12301.cc/module/wcb/adtest/zadmin.php',check, function(res){
					//$("#accountname").val(res.accountname);
					$(".adstatus").val(res.adstatus);	
					$(".forbiden").val(res.forbiden);	
					$("#max_debt").val(res.max_debt);
					$("#delay_days").val(res.delay_days);
					$("#proof_price").val(res.proof_price);
					$("#pricing_package").val(res.package_order_num);
					$("#package_order_num").val(res.pricing_package);
					$("#package_startdate").val(res.package_startdate);
					$("#package_enddate").val(res.package_enddate);
				},'json');
			} else {
				
			}
			uname = $(this).attr('data-name'); 
			content = '<form id="account_setting" action="ddddd">';
			content += '<p><input style="display:none" name="action" value="save"></p>';
			content += '<p><input style="display:none" name="accountid" value="'+guid+'"></p>';
			content += '<p><span class="set_acc">账户ID：</span><input class="pinput-txt_t" disabled="disabled" type="text" id="accountid" name="accountid" value="'+guid+'"></p>';
			content += '<p><span class="set_acc">账户名称：</span><input class="pinput-txt_t" type="text" id="accountname" name="accountname" value="'+uname+'"></p>';
			content += '<p><span class="set_acc">账户状态：</span><select id="com_type_t" class="adstatus" name="adstatus" onChange="change()">'+options+'</select><span class="set_ac">&nbsp;禁用原因：</span><select id="com_type_ddt" class="forbiden" name="forbiden">'+options2+'</select></p>';
			content += '<p><span class="set_acc">允许钱款金额：</span><input class="pinput-txt_t NumDecText" type="text" id="max_debt" name="max_debt" value="">&nbsp;元</p>';
			content += '<p><span class="set_acc">允许欠款时长:</span><input class="pinput-txt_t NumDecText" type="text" id="delay_days" name="delay_days" value="">&nbsp;天</p>';
			content += '<p><span class="set_acc">凭证费:</span><input class="pinput-txt_t NumDecText" type="text" id="proof_price" name="proof_price" value="">&nbsp;元/单</p>';
			content += '<p><span class="set_acc">凭证费套餐标准:</span><input class="pinput-txt_t NumDecText" type="text" id="pricing_package" name="pricing_package" value="">&nbsp;元</p>';
			content += '<p><span class="set_acc">套餐凭证数 :</span><input class="pinput-txt_t NumDecText" type="text" id="package_order_num" name="package_order_num" value="">&nbsp;个</p>';
			content += '<p><span class="set_acc">套餐有效期:</span><input class="pinput-txt_dt" onClick="WdatePicker()" data-t="bt"   type="text" id="package_startdate" name="package_startdate" value="">至<input class="pinput-txt_dt" type="text" id="package_enddate" name="package_enddate" data-t="et" onClick="WdatePicker()"  value=""></p>';
			content += '<i class="calendarBtn c1"></i>';
			content += '<i class="calendarBtn c2"></i>';
			content +='</form>';
			easyDialog.open({
				container : {
				header : '账户设置',
				content : content,
				yesFn :function(){
						var data = $("#account_setting").serialize();
						$.post("http://www.12301.cc/module/wcb/adtest/zadmin.php",data,function(data){
							
							if(data.outcome=='fail'){
								alert(data.msg);
								return false;
							}
							alert(data.msg);
							window.location.reload();
						},'json');
						
						
											
											
						
					},
				noFn : true
				}
				
			});
			$(".NumDecText").keyup(function(){   
				$(this).val($(this).val().replace(/[^0-9.]/g,''));    
			}).bind("paste",function(){  //CTR+V事件处理    
				$(this).val($(this).val().replace(/[^0-9.]/g,''));     
			})  
    });
	
	//联盟
	$("#mem_list_tbd").on('click','a.alliance', function(){
        guid = $(this).attr('data-id');
		
        uname = $("#editInfo_"+guid).find('span.dname').text(),
        content = '<form class="alliance">';
        content += '<input type="hidden" name="action" value="transform" />';
        content += '<input type="hidden" name="memberID" value="'+guid+'" />';
        content += '<p>联盟名称：<input class="pinput-txt" type="text" value="' + $.trim(uname) + '"  name="alliName"/></p>',
		// 平台|供应商|盟主|省代|市代|上级邀请人|邀请人
        content += '<p>利润比例：平台<input class="pinput-txt_b" type="text" name="profit_a" value="15">% 供应商<input class="pinput-txt_b" type="text" name="profit_b" value="20">% 联盟<input class="pinput-txt_b" type="text" name="profit_x">% 邀请人<input class="pinput-txt_b" type="text" name="profit_y">% </p>';
        content += '<p>联盟分配：盟主<input class="pinput-txt_b" type="text" name="profit_c" value="25">% 省代理<input class="pinput-txt_b" type="text" name="profit_d" value="15">% 市代理<input class="pinput-txt_b" type="text" name="profit_e" value="10">% </p>';
		content += '<p>邀请人分配：上级邀请人<input class="pinput-txt_b" type="text" name="profit_f" value="10">% 邀请人<input class="pinput-txt_b" type="text" name="profit_g" value="5">%</p>';
        // content += '<p><h4>（盟主：代理商：邀请人）</h4></p>';
        content +='</form>';
        // console.log(content);
        easyDialog.open({
            container : {
                header : '转化联盟',
                content : content,
                yesFn : function(){
                    var data = $(".alliance").serialize();
                    $.getJSON('../call/jh_alliance.php',data,function(json){
                        if(json.code!=100){
                            alert(json.msg);
                            return false;
                        }
                        alert(json.msg);
                        window.location.reload();
                    });
                },
                noFn : true
            }
        });
    });
    $("#mem_list_tbd").on('blur','input.dname_input', function(){
        $(this).attr('readonly','readonly').removeClass('edit_dname');
    });
    $("#ExcelBtn").on('click',function(){
        $("#act").val("loadExcel");
        $(".search_form ").submit();
    });
    $("#searchBtn").click(function(){
        $("#act").val("");
        $(".search_form").submit();
    });
    $("#mem_list_tbd").on('click', 'a.verify', function(){
        if(!confirm('您确定要执行此操作？')) return;
        var status = $(this).attr('data-status'),
            parent_obj = $(this).parent('td'),
            mid = parent_obj.siblings('td.mid').attr('data-mid'),
            params = {action:'SetMemStatus', status:status, mid:mid};
        $.post("/call/jh_mem.php", params, function(res){
            alert(res.msg);
            if(res.status=='ok') {
                parent_obj.siblings('td.status').html(res.body);
            }
        },'json');
    });
});

function change(){ 
	if($("#com_type_t").attr("value")=='1'){
		$("#com_type_ddt").val("0");
	}else{ 

	} 
} 
    
